﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MacomberMapSystem.Common.Processing;
using MacomberMapSystem.Common.Serialization;

namespace MacomberMapSystem.Common.User_Interfaces.Database
{
    public partial class ctlDailyUseReport : UserControl
    {
        /// <summary>The respository the control is associated with</summary>
        public MM_Repository Repository;

        /// <summary>
        /// Initialize a new daily use report
        /// </summary>
        public ctlDailyUseReport()
        {
            InitializeComponent();
            SetText("<h1>Daily use report</h1><h3>Please click 'refresh' to load a daily use report</h3>");
        }

        /// <summary>
        /// Set text for the web browser
        /// </summary>
        /// <param name="Html"></param>
        private void SetText(String Html)
        {
            wbBrowse.Navigate("about:blank");
            wbBrowse.Refresh();
            wbBrowse.Document.OpenNew(true);
            wbBrowse.Document.Write(Html);
        }

        /// <summary>
        /// Copy our HTML to our clipboard
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCopyToClipboard_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.Clear();
                Clipboard.SetText(wbBrowse.DocumentText, TextDataFormat.Text);
                Clipboard.SetText(wbBrowse.DocumentText, TextDataFormat.Html);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to copy to clipboard: " + ex.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Refresh our daily use report
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (Repository == null)
                MessageBox.Show("Unable to copy to produce report: No database repository set.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                try
                {
                    Dictionary<String,object> NamedParameters = new Dictionary<string,object>();
                    CIM_OneLine_Report Report = new CIM_OneLine_Report(null, NamedParameters, Repository);                    
                    Report.RunCompletion += RunCompleted;
                    Repository.ActiveProcesses.Add(Report);
                    Report.InitiateProcessInPooledThread(Repository.Db);
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to copy to produce report: " + ex.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
        }

        /// <summary>
        /// Handle our run completion
        /// </summary>
        /// <param name="OutputHtml"></param>
        private void RunCompleted(String OutputHtml)
        {
            if (InvokeRequired)
                Invoke(new CIM_OneLine_Report.ReportCompletionDelegate(RunCompleted), OutputHtml);
            else
                SetText(OutputHtml);
        }
    }
}
